<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"%>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>权限管理</title>
  <link rel="stylesheet" type="text/css" href="<%=path %>/style/easyui.css">
  <link rel="stylesheet" type="text/css" href="<%=path %>/style/icon.css">
  <link rel="stylesheet" type="text/css" href="<%=path %>/style/neweasyui/themes/gray/panel.css">
  <link rel="stylesheet" type="text/css" href="<%=path %>/style/neweasyui/themes/gray/window.css">
  
  <link rel="stylesheet" type="text/css" href="<%=path %>/script/zTree_v3/css/zTreeStyle/zTreeStyle.css" >
  		<link rel="stylesheet" type="text/css" href="<%=path %>/script/zTree_v3/css/demo.css" >
  
  <script type="text/javascript" src="<%=path %>/script/1.11.1.js"></script>
  <script type="text/javascript" src="<%=path %>/script/jquery.easyui.min.js"></script>
  <script type="text/javascript" src="<%=path %>/script/jquery.easyui.min.js"></script>
  <script type="text/javascript" src="<%=path %>/script/easyui-lang-zh_CN.js"></script>
  <script type="text/javascript" src="<%=path %>/script/dateFormat.js"></script>
  <script type="text/javascript" src="<%=path %>/script/datagrid-bufferview.js"></script>
  <script type="text/javascript" src="<%=path %>/script/common.js"></script>
  <script type="text/javascript" src="<%=path %>/script/ckeditor/ckeditor.js"></script>
  
  <script type="text/javascript" src="<%=path %>/script/zTree_v3/js/jquery.ztree.core-3.5.js"></script>  
		<script type="text/javascript" src="<%=path %>/script/zTree_v3/js/jquery.ztree.excheck-3.5.js"></script>  
		<script type="text/javascript" src="<%=path %>/script/zTree_v3/js/jquery.ztree.exedit-3.5.js"></script> 
  <style type="text/css">
    #fm{
      margin:0;
      padding:10px 30px;
    }
    .ftitle{
      font-size:14px;
      font-weight:bold;
      padding:5px 0;
      margin-bottom:10px;
      border-bottom:1px solid #ccc;
    }
    .fitem{
      margin-bottom:17px;
    }
    .fitem label{
      display:inline-block;
      width:80px;
    }
    .fitem input{
      width:230px;
    }
  </style>
</head>
<body>
<table id="tt" title="class" style="width:auto;height:840px">
</table>
<!-----------------------------------------------------------------按钮------------------------------------------------------------------->
<div id="toolbar" style="padding:5px;height:auto">
  <div>
  	<!--   父ID：<input id="parentId"  name="parentId" type="text" class="easyui-numberbox" style="width:150px" validType="length[0,10]" missingMessage="必须为数字" />&nbsp;&nbsp;
  	 权限名称：<input id="rightName"  name="rightName" type="text" class="easyui-textbox" validType="length[0,10]" style="width:150px" />&nbsp;&nbsp;
    <a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-search" onclick="searchSentence()">查询</a>-->
  
    <!-- 搜索条件结束 -->
    <a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-add" onclick="addAd()">添加</a>&nbsp;
    <a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-edit" onclick="editClass()">修改</a>&nbsp;
    <a href="javascript:void(0)"  class="easyui-linkbutton" iconCls="icon-remove" onclick="deleteClass()">删除</a>&nbsp;&nbsp;

  </div>
</div>
<!-------------------------------------------------------------添加/编辑框 开始----------------------------------------------------------------------->
<!-- 添加/编辑框 开始-->
<div id="addad" class="easyui-dialog"
     style="width: 800px; height: 500px; padding: 10px 20px;"  closed="true"
     buttons="#dlg-buttons">
  <div class="ftitle">权限</div>
  <form id="inAddAd" method="post"  validate enctype="multipart/form-data">
    <input type="hidden" name="id" id="cls_id" />
    <div class="fitem" style="display:none;">
      <label>父ID:</label>
      <input  class="easyui-textbox" name="pId" id="pId" value=""  validate validType="length[0,11]"/>
    </div>
    <div class="fitem">
      <label>权限名称:</label>
      <input  class="easyui-textbox" name="name" id="name" value=""  validate required validType="length[0,64]"/>
    </div>
    <div class="fitem">
      <label>资源路径:</label>
      <textarea  rows="3" cols="10"  style="width: 80%" class="textarea easyui-validatebox"  id="urlContent" name="urlContent" validate validType="length[0,512]"></textarea>
    </div>
    <div class="fitem">
      <label>描述:</label>
      <textarea  rows="3" cols="10"  style="width: 80%" class="textarea easyui-validatebox" id="description" name="description" validate validType="length[0,200]"></textarea>
    </div>
    
  </form>
  <div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6"  iconCls="icon-ok" onclick="saveOrUpdateAd()" style="width: 90px">Save</a> 
    <a href="javascript:void(0)" class="easyui-linkbutton"  iconCls="icon-cancel" onclick="closeDialog()" style="width: 90px">Cancel</a>
  </div>
</div>
<!------------------------------------------------------------------------------------------------------------------------------------>

<!-- 添加/编辑框结束-->





<script type="text/javascript">
  $.ajaxSetup({cache:false,async:false});
  var treeNodeId = (<%=request.getSession().getAttribute("id")%>==null)?1:<%=request.getSession().getAttribute("id")%>
  $(function(){
    $('#tt').datagrid({
      url:'<%=basePath %>authority/right/detail/'+treeNodeId,
      method:'get',
      singleSelect:true,toolbar:'#toolbar',remoteSort:false,pageSize:50,nowrap:false,
      fitColumns:true,pagination:true,rownumbers:true,
      columns:[[
        {field:'id',title:'ID',sortable:true,width:20,hidden:true},
        {field:'pId',title:'父ID',sortable:true,width:30,hidden:true},
        {field:'name',title:'资源名',sortable:true,width:200},
        {field:'urlContent',title:'资源路径',sortable:true,width:200},
        {field:'description',title:'描述',sortable:true,width:37},
      ]],
    
    });
  });
//------------------------------------------------------------------------------------------
  //关闭弹出框
  function closeDialog(){
    $('#addad').dialog('close') ;
  }

  //添加权限
  function addAd(){
	var pId = "<%=request.getSession().getAttribute("id")%>";
	if(pId == null || pId == "null"){
		pId = "1";
	}
	$("#pId").val(pId);
    $('#addad').dialog('open').dialog('setTitle','添加权限');
    $('#inAddAd').form('load',{'pId':$("#pId").val()});
    $('#inAddAd').form('load',{'name':'','urlContent':'','description':''});
    url = '<%=basePath%>authority/right/add';
  }
  //修改权限
  function editClass(){
    url = '<%=basePath%>authority/right/edit';
    var row = $('#tt').datagrid('getSelected');
    if (row){
      $("#cls_id").val(row.id);
      $("#pId").val(row.pId);
      $("#name").val(row.name);
      $("#urlContent").val(row.urlContent);
      $("#description").val(row.description);
      $('#addad').dialog('open').dialog('setTitle','权限编辑');
      $('#inAddAd').form('load',row);
    }
  }


  function deleteClass(){
	
    var row = $('#tt').datagrid('getSelected');
    if (row){
      $.messager.confirm('提示','确定要删除这个权限吗？',function(r){
        if (r){
          $.post('<%=basePath%>authority/right/deleteone',{id:row.id},function(result){
        	if(result.status == -2){
        		$.messager.alert("提示","此权限被使用,不能被删除");
        	}
            $('#tt').datagrid('reload');    // reload the data
          },'json');
          window.parent.frames['rightLeft'].location.reload();
        }
      });
    }
  }
//---------------------------------------------------------------------------------------------------------------------------
  //添加或修改权限
  function saveOrUpdateAd(){

    $('#inAddAd').form('submit', {
      url : url,
      onSubmit : function() {
        return $(this).form('validate');
      },
      success : function(result) {
    	var Data = eval('(' + result + ')');   	  
        if (result.status) {
          $.messager.show({
            title : 'Error',
            msg : '服务器出错，请稍后再试'
          });
        }else if(Data.status == '-1'){
        	$.messager.show({
                title : '提示',
                msg : '此时有正在活动的权限,请先调整'
              });
        	return;
        }else if(Data.status == '-2'){
        	$.messager.show({
                title : '提示',
                msg : '其并列节点存在同名节点'
              });
        	return;
        }else {
          $('#addad').dialog('close'); // close the dialog
          $('#tt').datagrid('reload'); // reload the data
          //重新加载树接口 ：注意 下边代码中frames['rightLeft'] 中的引号是frameset的name属性
          window.parent.frames['rightLeft'].location.reload();
        }
      }
    });
  }



  function searchSentence(){
	  var parentId = $("#parentId").textbox("getValue");
	  var rightName = $("#rightName").textbox("getValue");
		 var url = '<%=basePath %>sentence/listAll?parentId='+parentId+'&rightName='+rightName;
		 url =encodeURI(encodeURI(url));
		 $("#tt").datagrid({
			 url:url,
			 method:'get',
			 onLoadSuccess:function(data){
				 if(data.total == null){
					 $.messager.alert("提示","没有查询到任何符合条件的数据")
				 }
			 }
		 });
  }
 
	
  

</script>
</body>
</html>
